#include <stdint-gcc.h>

#include "uart.h"
#include "timer.h"

extern const volatile uint8_t pushbutton1;

int xmodem_recv_packet(int pktno, uint8_t packet[128]);

void main(void)
{
	int pktno;
	uint8_t *p;

	uart_lcr = 0x83;
	uart_dl  = 54;
	uart_lcr = 0x03;

	uart_print("Serial bootloader ready.\r\n");

retry:
	while (!pushbutton1);

	uart_out(21);

	pktno = 1;
	p = 0;
	for (;;) {
		int ret = xmodem_recv_packet(pktno, p);
		if (ret < 0) goto retry;
		if (ret == 0) break;
		pktno++;
		p += 128;
	}

	uart_print("Image received.\r\n");
	((void(*)())0)();
}
